package com.ruoyi.system.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.pingan.openbank.api.sdk.client.ApiClient;
import com.pingan.openbank.api.sdk.entity.SdkRequest;
import com.ruoyi.system.config.PayConfig;

import java.util.Date;

public class PaPayUtil {
    //public final static ApiClient apiClient = ApiClient.getInstance("conf/config.properties");
    public volatile static ApiClient apiClient;

    public PaPayUtil() {
    }

    public static String code_000000 = "000000";
    public static String code = "Code";
    public static String TXN_RETURN_CODE = "TxnReturnCode";
    public static String TXN_RETURN_MSG = "TxnReturnMsg";
    public static String extenddata = "ExtendData";
    public static String CNSMR_SEQ_NO = "CnsmrSeqNo";
    public static String edata = "Data";
    public static String errors = "Errors";
    public static String errorcode = "ErrorCode";
    public static String errormessage = "ErrorMessage";
    public static String message = "Message";
    public static String CHANNEL_ORDER_NO = "ChannelOrderNo";

    /**
     * 确认收货标识
     */
    public static String CONFIRM_FLAG = "1";

    public static String CNSMR_SEQ_NO_PREFIX = "F25634";

    /**
     * 交易网会员代码_采购商(user)
     */
    public static String TRAN_NET_MEMBER_CODE_U = "u";
    /**
     * 交易网会员代码_批发商(shop)
     */
    public static String TRAN_NET_MEMBER_CODE_S = "s";
    /**
     * 实名开户-KFEJZB6248
     */
    public static String AUTONYM_OPEN_CUSTACCT_ID = "/V1.0/AutonymOpenCustAcctId";

    /**
     * 登记行为记录信息-KFEJZB6244
     */
    public static String REGISTER_BEHAVIOR_RECORD = "/V1.0/RegisterBehaviorRecordInfo";

    /**
     * 会员绑定提现账户小额鉴权-校验法人-KFEJZB6240
     */
    public static String BIND_SMALL_AMOUNT_WITH_CHECK_CORP = "/V1.0/BindSmallAmountWithCheckCorp";

    /**
     * 小额鉴权回填金额-校验法人-KFEJZB6241
     */
    public static String CHECK_AMOUNT_WITH_CORP = "/V1.0/CheckAmountWithCorp";

    /**
     * 会员解绑提现账户-KFEJZB6065
     */
    public static String UNBIND_RELATE_ACCT = "/V1.0/UnbindRelateAcct";

    /**
     * 查询小额鉴权转账结果-KFEJZB6061
     */
    public static String Small_Amount_Transfer_Query = "/V1.0/SmallAmountTransferQuery";

    /**
     * 见证子台帐信息查询-KFEJZB6324
     */
    public static String EJZB_Cust_Information_Query = "/V1.0/EJZBCustInformationQuery";

    /**
     * 会员绑定信息查询-KFEJZB6098
     */
    public static String MEMBER_BIND_QUERY = "/V1.0/MemberBindQuery";

    /**
     * 根据会员代码查询会员子账号-KFEJZB6092
     */
    public static String QUERY_CUST_ACCTID_BY_THIRDCUSTID = "/V1.0/QueryCustAcctIdByThirdCustId";

    /**
     * 维护会员绑定提现账户联行号-KFEJZB6138
     */
    public static String MNT_MBR_BIND_RELATE_ACCT_BANK_CODE = "/V1.0/MntMbrBindRelateAcctBankCode";

    /**
     * 申请修改手机号码-KFEJZB6083
     */
    public static String APPLY_FOR_CHANGEOF_CELL_PHONE_NUM = "/V1.0/ApplyForChangeOfCellPhoneNum";

    /**
     * 冻结充值-KFEJZB6267
     */
    public static String THIRD_FREEZE_RECHARGE = "/V1.0/ThirdFreezeRecharge";

    /**
     * 冻结充值撤销-KFEJZB6268
     */
    public static String THIRD_FREEZE_RECHARGE_CANCEL = "/V1.0/ThirdFreezeRechargeCancel";

    /**
     * 补贴-KFEJZB6269
     */
    public static String SUBSIDY_QUEST = "/V1.0/SubsidyQuest";

    /**
     * 补贴撤销-KFEJZB6270
     */
    public static String SUBSIDY_CANCEL = "/V1.0/SubsidyCancel";

    /**
     * 查询银行子账户余额-KFEJZB6010
     */
    public static String CUST_ACCT_ID_BALANCE_QUERY = "/V1.0/CustAcctIdBalanceQuery";

    /**
     * 分账-KFEJZB6271
     */
    public static String SUB_ACCOUNT_QUEST = "/V1.0/SubAccountQuest";

    /**
     * 分账撤销-KFEJZB6272
     */
    public static String SUB_ACCOUNT_CANCEL = "/V1.0/SubAccountCancel";

    /**
     * 登记挂账(支持撤销)-KFEJZB6139
     */
    public static String REGISTER_BILL_SUPPORT_WITHDRAW = "/V1.0/RegisterBillSupportWithdraw";

    /**
     * 登记挂账撤销-KFEJZB6140
     */
    public static String REV_REGISTER_BILL_SUPPORT_WITHDRAW = "/V1.0/RevRegisterBillSupportWithdraw";

    /**
     * 交易终态查询-KFEJZB6273
     */
    public static String TRA_FINAL_STATUS_QUERY = "/V1.0/TraFinalStatusQuery";

    /**
     * 订单冻结余额查询-KFEJZB6292
     */
    public static String QUERY_CROWD_ACOUNT_MSG = "/V1.0/QueryCrowdAcountMsg";

    /**
     * 会员资金冻结-不验证-KFEJZB6007
     */
    public static String MEMBERSHIP_TRANCHE_FREEZE = "/V1.0/MembershipTrancheFreeze";

    /**
     * 会员间交易-不验证-KFEJZB6034
     */
    public static String MEMBER_TRANSACTION = "/V1.0/MemberTransaction";

    /**
     * 申请提现或支付短信动态码-KFEJZB6082
     */
    public static String APPLICATION_TEXT_MSG_DYNAMIC_CODE = "/V1.0/ApplicationTextMsgDynamicCode";


    /**
     * 会员提现-支持手续费-KFEJZB6085
     */
    public static String MEMBER_WITHDRAW_CASH = "/V1.0/MemberWithdrawCash";

    /**
     * 会员间交易-验证短信动态码-KFEJZB6101
     */
    public static String MEMBER_TRANVERIFY_TEXT_MSGS = "/V1.0/MemberTranVerifyTextMsgs";

    /**
     * 查询银行提现退单信息-KFEJZB6048
     */
    public static String BANK_WITHDRAW_CASH_BACK_QUERY = "/V1.0/BankWithdrawCashBackQuery";

    /**
     * 会员绑定提现账户银联鉴权-校验法人-KFEJZB6238
     */
    public static String BIND_UNION_PAY_WITH_CHECK_CORP = "/V1.0/BindUnionPayWithCheckCorp";

    /**
     * 银联鉴权回填短信码-校验法人-KFEJZB6239
     */
    public static String CHECK_MSG_CODE_WITH_CORP = "/V1.0/CheckMsgCodeWithCorp";

    /**
     * 查询会员子账号-KFEJZB6037
     */
    public static String QUERY_CUST_ACCT_ID = "/V1.0/QueryCustAcctId";

    /**
     * 指定转账划款（测试专用，不能投产）-KFEJZB6211
     */
    public static String APNT_TRANSFER = "/V1.0/ApntTransfer";

    /**
     * 查询资金汇总账户余额-KFEJZB6011
     */
    public static String SUP_ACCT_ID_BALANCE_QUERY = "/V1.0/SupAcctIdBalanceQuery";

    /**
     * 会员间交易退款-不验证-KFEJZB6164
     */
    public static String MEMBER_TRANSACTION_REFUND = "/V1.0/MemberTransactionRefund";

    /**
     * 查询对账文件信息-KFEJZB6103
     */
    public static String RECONCILIATION_DOCUMENT_QUERY = "/V1.0/ReconciliationDocumentQuery";

    /**
     * 查询银行时间段内交易明细-KFEJZB6072
     */
    public static String BANK_TRANSACTION_DETAILS_QUERY = "/V1.0/BankTransactionDetailsQuery";

    /**
     * 查询银行时间段内清分提现明细-KFEJZB6073
     */
    public static String BANK_WITHDRAW_CASH_DETAILS_QUERY = "/V1.0/BankWithdrawCashDetailsQuery";

    /**
     * 查询普通转账充值明细-KFEJZB6050
     */
    public static String COMMON_TRANSFER_RECHARGE_QUERY = "/V1.0/CommonTransferRechargeQuery";

    /**
     * 查询会员子账号余额-KFEJZB6093
     */
    public static String QUERY_CUST_ACCT_ID_BALANCE = "/V1.0/QueryCustAcctIdBalance";

    /**
     * 查询子帐号历史余额及待转可提现状态信息-KFEJZB6114
     */
    public static String CUST_ACCT_ID_HISTORY_BALANCE_QUERY = "/V1.0/CustAcctIdHistoryBalanceQuery";


    /**
     * 查询明细单验证码-KFEJZB6142
     */
    public static String DETAIL_VERIFIED_CODE_QUERY = "/V1.0/DetailVerifiedCodeQuery";

    /**
     * 查询银行在途清算结果-KFEJZB6108
     */
    public static String BANK_CLEAR_QUERY = "/V1.0/BankClearQuery";

    /**
     * 查询银行单笔交易状态-KFEJZB6110
     */
    public static String SINGLE_TRANSACTION_STATUS_QUERY = "/V1.0/SingleTransactionStatusQuery";

    /**
     * 查询银行费用扣收结果-KFEJZB6109
     */
    public static String BANK_COST_DS_DEAL_RESULT_QUERY = "/V1.0/BankCostDsDealResultQuery";

    /**
     * 查询充值明细-见证收单-KFEJZB6146
     */
    public static String CHARGE_DETAIL_QUERY = "/V1.0/ChargeDetailQuery";

    /**
     * 调账-见证收单-KFEJZB6145
     */
    public static String ACCOUNT_REGULATION = "/V1.0/AccountRegulation";

    /**
     * 平台补账-见证收单-KFEJZB6147
     */
    public static String PLATFORM_ACCOUNT_SUPPLY = "/V1.0/PlatformAccountSupply";


    public static HttpResult post(Object object, String url) {
        SdkRequest sdkRequest = new SdkRequest();
        sdkRequest.setInterfaceName(url);
        sdkRequest.setBody(object);
        return getInstance().invoke(sdkRequest);
    }

    public static ApiClient getInstance() {
        if (apiClient == null) {
            synchronized (PaPayUtil.class) {
                if (apiClient == null) {
                    PayConfig payConfig = SpringUtil.getBean(PayConfig.class);
                    if (payConfig.getTest()) {
                        apiClient = ApiClient.getInstance("conf/configTest.properties");
                    } else {
                        apiClient = ApiClient.getInstance("conf/config.properties");
                    }
                }
            }
        }
        return apiClient;
    }

    /**
     * 识别个体户或者工商户
     *
     * @param chart
     * @return 2：企业商户；1：个体工商户
     */
    public static String getCreditType(String chart) {
        String str = chart.substring(0, 2);
        if (str.equals("91") || str.equals("93")) {
            return "2";
        }
        return "1";
    }

    public static String getCnsmrSeqNo() {
        return CNSMR_SEQ_NO_PREFIX + DateUtil.format(new Date(), "YYYYMM") + RandomUtil.randomNumbers(10);
    }
}
